﻿
Public Class frmNuevaPatente
    Inherits frmMaster

    Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            txtAnno.Attributes.Add("onkeypress", "return isNumber(event)")
            txtAnno.Attributes.Add("onkeydown", "return isNumber(event)")
            txtPatente.Attributes.Add("onkeydown", "return isAlphaNumber(event);this.value=this.value.toUpperCase();")
            txtPatente.Attributes.Add("onkeypress", "return isAlphaNumber(event);this.value=this.value.toUpperCase();")
            txtPatente.Attributes.Add("style", "text-transform: uppercase")

            lblRUTCliente.Text = RUTCliente
            lblNombre.Text = NombreCliente
            lblEmail.Text = Email

            CargarTipoVehiculo()

            CargarMarcas()
        End If
    End Sub

    Protected Sub btnVolver_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnVolver.Click
        IrA("frmListadoSeguros.aspx")
    End Sub

    Protected Sub btnConfirmacion_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConfirmacion.Click
        Dim sErrores As String = String.Empty

        If txtPatente.Text.Trim <> "" Then
            If txtPatente.Text.Trim.Length < 6 Then
                sErrores += "Largo de patente no válido.-" & "\n"
            Else
                If Not ValidarPatente(txtPatente.Text) Then
                    sErrores += "Ingrese una patente válida.-" & "\n"
                End If
            End If
        Else
            sErrores += "Ingrese patente.-"
        End If

        If ddlTipoVehiculo.SelectedIndex = 0 Then
            sErrores += "Seleccione Tipo Vehículo.-" & "\n"

        End If

        If ddlTipoVehiculo.SelectedIndex = 0 Then
            sErrores += "Seleccione Tipo Vehículo.-" & "\n"
        End If

        If ddlMarca.SelectedIndex = 0 Then
            sErrores += "Seleccione Marca.-" & "\n"

        End If

        If ddlModelo.SelectedIndex = 0 Then
            sErrores += "Seleccione Modelo.-" & "\n"
        End If

        If txtAnno.Text.Trim = "" Then
            sErrores += "Ingrese Año.-" & "\n"
        Else
            If CType(txtAnno.Text.Trim, Integer) >= DateTime.Now.Year + 1 Then
                sErrores += "Ingrese Año válido.-" & "\n"
            End If
        End If

        If sErrores.Trim = "" Then
            NroSeguro = ""
            Patente = txtPatente.Text.Trim.ToUpper
            TipoVehiculo = ddlTipoVehiculo.SelectedItem.Text
            Marca = ddlMarca.SelectedItem.Text
            Modelo = ddlModelo.SelectedItem.Text
            Anio = txtAnno.Text.Trim

            IrA("frmConfirmacion.aspx")
        Else
            MsgError(sErrores)
        End If
    End Sub

    Private Sub CargarModelos(ByVal ItemTipoVehiculo As ListItem, ByVal ItemMarca As ListItem)
        CargarModelos(ddlModelo, ItemTipoVehiculo, ItemMarca)
    End Sub

    Private Sub CargarModelos(ByVal ddl As Anthem.DropDownList, ByVal ItemTipoVehiculo As ListItem, ByVal ItemMarca As ListItem)
        CargarModelos(ddl, ItemTipoVehiculo.Value, ItemMarca.Value)
    End Sub

    Private Sub CargarModelos(ByVal ddl As Anthem.DropDownList, ByVal IdTipoVehiculo As String, ByVal IdMarca As String)
        Dim PARM(1) As String

        PARM(0) = IdMarca
        PARM(1) = IdTipoVehiculo

        CargaDDL(ddl, "SP_GET_Modelo", "modelo", "modelo", PARM)
    End Sub

    Private Function BuscarModelosByMarca(ByVal Marca As String) As DataTable
        Dim dtModelos As DataTable = Nothing

        Return dtModelos
    End Function

    Private Function BuscarMarca() As DataTable
        Dim dtMarca As DataTable = Nothing

        Return dtMarca
    End Function

    Private Sub CargarMarcas()
        CargarMarcas(ddlMarca)

        CargarModelos(ddlTipoVehiculo.SelectedItem, ddlMarca.SelectedItem)
    End Sub

    Private Sub CargarMarcas(ByVal ddl As Anthem.DropDownList)
        CargaDDL(ddl, "SP_GET_Marca", "cod_marca", "descripcion_marca")
    End Sub

    Protected Sub ddlMarca_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlMarca.SelectedIndexChanged
        CargarModelos(ddlTipoVehiculo.SelectedItem, ddlMarca.SelectedItem)
    End Sub

    Private Sub CargarTipoVehiculo()
        CargaDDL(ddlTipoVehiculo, "SP_GET_TipoVehiculo", "codigo_tipo", "descripcion_tipo")
    End Sub

    Protected Sub ddlTipoVehiculo_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlTipoVehiculo.SelectedIndexChanged
        ddlModelo.SelectedIndex = 0
        ddlModelo.UpdateAfterCallBack = True
        CargarMarcas()
    End Sub
End Class